Xbasic

Modifying Object Event Code on the Fly

Description

Using Xbasic, you can access and modify the scripts that are attached to object events. To view the script that is attached to an object event, use the object's Code property. For example, assume that you have a form called "Customer" with a button called "Button1". To see the Xbasic script that is attached to the button's OnPush event, type this command in the Interactive window:

? customer:button1.code.onpush

You can also assign new code to an event by setting the object's code property. The change is in effect until the window is closed. When the window is reopened after having been closed, the event code is restored to its initial state.

The following example shows how a button's OnPush event code is stored in a variable. The code is then changed.

Code = :customer:button1.code.onpush
:customer:button1.code.onpush = "ui_msg_box(\"This is temporary code.\", \"It does nothing\") "

To restore the original code, you would execute this command:

:customer:button1.code.onpush = Code

In addition to the Code property, each object also has an Event property which provides a convenient way to temporarily override the script that is attached to an object event. Each object's Event property is initially NULL. To override an event's script, set the Event property. To restore the original script, clear the Event property.

For example, assume you have a form called "Customer", with a button called "Button1". The code for this button's OnPush event is:

topparent.new_record()
city.value = "Boston"
state.value = "MA"

To temporarily override this code with different code, you could execute the following statements:

code = "topparent.new_record()" + CRLF()+ "city.value = \"New York\" "
customer:button1.event.OnPush = code

To restore the original event code, execute this statement:

customer:button1.event.OnPush = ""